package cn.edu.zjusc.ExcelGenerator;

//import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
//import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.List;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.CellStyle;
//import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

import cn.edu.zjusc.ExcelModel.StudentGroupInfo;

public class GroupGenerator {
	/**
	 * The spreadsheet to read in
	 */
	//private File inputWorkbook;
	/**
	 * The spreadsheet to output
	 */
	//private File outputWorkbook;

	InputStream inp;
	FileOutputStream fileOut;
	//private SimpleDateFormat mDateFormat = new SimpleDateFormat("yyyy-MM-dd");

	Workbook wb;

	public void modify(String input, String output,
			List<StudentGroupInfo> studentList, String year, String season,
			int groupExamId, String subject, Date date, int duration)
			throws InvalidFormatException, IOException {
		inp = new FileInputStream(input);

		wb = WorkbookFactory.create(inp);

		completeData(input, output, studentList, year, season, groupExamId,
				subject, date, duration);
		fileOut = new FileOutputStream(output);
		wb.write(fileOut);
		fileOut.close();
	}

	private void completeData(String input, String output,
			List<StudentGroupInfo> studentList, String year, String season,
			int groupExamId, String subject, Date date, int duration) {
		CellStyle style1 = wb.createCellStyle();
		CellStyle style2 = wb.createCellStyle();
		// 表格边线
		// 底边线
		style1.setBorderBottom(CellStyle.BORDER_THIN);
		// 左边线
		style1.setBorderLeft(CellStyle.BORDER_THIN);
		style1.setAlignment(CellStyle.ALIGN_CENTER);
		style1.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
		// 底边线
		style2.setBorderBottom(CellStyle.BORDER_THIN);
		// 左边线
		style2.setBorderLeft(CellStyle.BORDER_THIN);
		// 右边线
		style2.setBorderRight(CellStyle.BORDER_THIN);
		style2.setAlignment(CellStyle.ALIGN_CENTER);
		style2.setVerticalAlignment(CellStyle.VERTICAL_CENTER);

		System.out.println("----------------------开始生成表格");
		int listSize = studentList.size();
		//CreationHelper createHelper = wb.getCreationHelper();
		Sheet sheet = wb.getSheetAt(0);
		// 循环创建cell，添加数据
		for (int i = 0; i < listSize; i++) {
			// Create a row and put some cells in it. Rows are 0 based.
			Row row = sheet.createRow((short) (i + 3));
			row.setHeightInPoints(25);
			row.createCell(0).setCellValue(
					studentList.get(i).getAdmissionTicketID());
			row.getCell(0).setCellStyle(style1);
			row.createCell(1).setCellValue(studentList.get(i).getName());
			row.getCell(1).setCellStyle(style1);
			row.createCell(2).setCellValue(studentList.get(i).getMajor());
			row.getCell(2).setCellStyle(style1);
			row.createCell(3).setCellValue(studentList.get(i).getSeatNo());
			row.getCell(3).setCellStyle(style2);
		}
		if (season.equals("0")) {
			season = "春季";
		} else {
			season = "秋季";
		}
		int startHour = date.getHours();
		int startMintue = date.getMinutes();
		int endHour = duration / 60 + startHour;
		int endMintue = duration % 60 + startMintue;
		if (endMintue >= 60) {
			endHour++;
			endMintue = endMintue - 60;
		}
		String endMintueString = null;
		String timeStamp = null;
		if (endMintue == 0) {
			endMintueString = "00";
		} else {
			endMintueString = Integer.toString(endMintue);
		}
		if (startMintue != 0) {
			timeStamp = Integer.toString(startHour) + ":"
					+ Integer.toString(startMintue) + "-"
					+ Integer.toString(endHour) + ":" + endMintueString;
		} else {
			timeStamp = Integer.toString(startHour) + ":" + "00" + "-"
					+ Integer.toString(endHour) + ":" + endMintueString;
		}

		sheet.getRow(0)
				.getCell(0)
				.setCellValue(
						"浙江大学" + year + "年" + season + "攻读软件工程硕士专业学位入学考试组考表");
		sheet.getRow(1)
				.getCell(0)
				.setCellValue(
						"考场号：" + groupExamId + "(   )  " + "考试科目：" + subject
								+ "  考试时间：" + date.toString().substring(0, 10)
								+ " " + timeStamp);
	}
	/**
	 * @param args
	 * @throws IOException
	 * @throws InvalidFormatException
	 * @throws NamingException
	 */
	// public static void main(String[] args) throws InvalidFormatException,
	// IOException, NamingException {
	// // TODO Auto-generated method stub
	//
	// GroupGenerator generator=new GroupGenerator();
	// List<StudentGroupInfo> studentList=new ArrayList<StudentGroupInfo>();
	// StudentGroupInfo studentGroupInfo1=new
	// StudentGroupInfo("1111","lb","ruanfu",1);
	// studentList.add(studentGroupInfo1);
	// StudentGroupInfo studentGroupInfo2=new
	// StudentGroupInfo("22222","lb","ruanfu",1);
	// studentList.add(studentGroupInfo2);
	// StudentGroupInfo studentGroupInfo3=new
	// StudentGroupInfo("3333","lb","ruanfu",1);
	// studentList.add(studentGroupInfo3);
	//
	// generator.modify("./WebContent/exam_group.xls",
	// "./WebContent/exam_groupOK.xls", studentList);
	//
	// }

}
